/*
 * FlagView.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;

/** 商品左上角 旗标 */
public class FlagView extends RelativeLayout {

    /** 赠品 */
    public static final int TYPE_GIFT = 0;

    /** 减免 */
    public static final int TYPE_REDUCE = 1;

    /** 折扣 */
    public static final int TYPE_DISCOUNT = 2;

    private ImageView mIvType;

    private TextView mTvName;

    public FlagView(Context context) {
        this(context, null);
    }

    public FlagView(Context context, AttributeSet attrs) {
        super(context, attrs);

        View root = View.inflate(context, R.layout.flag_view, this);

        mIvType = (ImageView) root.findViewById(R.id.iv_type);
        mTvName = (TextView) root.findViewById(R.id.tv_name);
    }

    private void setType(int type) {
        int resId = -1;
        switch (type) {
            case TYPE_GIFT:
                resId = R.mipmap.ic_flag_gift;
                break;
            case TYPE_DISCOUNT:
                resId = R.mipmap.ic_flag_discount;
                break;
            case TYPE_REDUCE:
                resId = R.mipmap.ic_flag_reduce;
                break;
        }
        mIvType.setBackgroundResource(resId);
    }

    public void setFlagText(String text, int type) {
        if (TextUtils.isEmpty(text)) {
            setVisibility(GONE);
        } else {
            setVisibility(VISIBLE);
            mTvName.setText(text);
            setType(type);
        }
    }
}
